package com.thirtydays.frame.util

import android.content.Context
import android.graphics.Color
import android.widget.TextView
import androidx.viewpager2.widget.ViewPager2
import com.thirtydays.frame.R
import com.thirtydays.frame.util.ext.dp2pxFloat
import com.thirtydays.frame.util.ext.dp2pxInt
import com.thirtydays.frame.util.ext.getDimen
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView


object IndicatorUtils {
    fun showSizeIndicator(
        context: Context, indicator: MagicIndicator, viewPager2: ViewPager2, isAdjustMode: Boolean = true
    ) {

        val commonNavigator = CommonNavigator(context)
        commonNavigator.leftPadding = dp2pxInt(15f)
        commonNavigator.rightPadding = dp2pxInt(15f)
        commonNavigator.isAdjustMode = isAdjustMode
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount() = 0

            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                val commonPagerTitleView = CommonPagerTitleView(context)
                commonPagerTitleView.setContentView(R.layout.indicator_item)
                val tvTab = commonPagerTitleView.findViewById<TextView>(R.id.tvTab)
                commonPagerTitleView.onPagerTitleChangeListener = object : CommonPagerTitleView.OnPagerTitleChangeListener {
                    override fun onSelected(index: Int, totalCount: Int) {
                        tvTab.setTextColor(Color.RED)
                        tvTab.textSize = 17f
                    }

                    override fun onDeselected(index: Int, totalCount: Int) {
                        tvTab.setTextColor(Color.BLUE)
                        tvTab.textSize = 14f
                    }

                    override fun onLeave(index: Int, totalCount: Int, leavePercent: Float, leftToRight: Boolean) {
                    }

                    override fun onEnter(index: Int, totalCount: Int, enterPercent: Float, leftToRight: Boolean) {
                    }
                }
                return commonPagerTitleView
            }

            override fun getIndicator(context: Context?): IPagerIndicator {
                val dp12 = context?.let {
                    val dimen12 = getDimen(R.dimen.spacing_12)
                    dimen12
                }

                val indicator = LinePagerIndicator(context)
                indicator.setColors(Color.RED)
                indicator.roundRadius = dp2pxFloat(2f)
                indicator.lineHeight = dp2pxFloat(2f)
                indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                return indicator
            }
        }

        indicator.navigator = commonNavigator
        viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                indicator.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                indicator.onPageSelected(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
                indicator.onPageScrollStateChanged(state)
            }
        })
    }
}